//
//  VMRefreshHeader.m
//  VMall
//
//  Created by ning liu on 17/5/23.
//  Copyright © 2017年 mohao. All rights reserved.
//

#import "VMRefreshHeader.h"

#define ImageWidth 55

@interface VMRefreshHeader ()
{
    UIImageView * _imgView;
    UIImageView * _leafView;
    
    
    NSMutableArray *_changeArray;
    NSMutableArray *_loadingArray;
    
}
@end

@implementation VMRefreshHeader

#pragma mark - 重写父类的方法

- (void)prepare
{
    [super prepare];
    
    _changeArray = [NSMutableArray new];
    for (int i =0; i<25; i++) {
        NSString *name = [NSString stringWithFormat:@"refresh_transform_%zd",i+23];
        UIImage *img = [UIImage imageNamed:name];
        [_changeArray addObject:img];
    }
    
    _loadingArray = [NSMutableArray new];
    for (int i =1; i<24; i++) {
        NSString *name = [NSString stringWithFormat:@"refresh_circle_%zd",i+24];
        UIImage *img = [UIImage imageNamed:name];
        [_loadingArray addObject:img];
    }
}

- (void)placeSubviews
{
    [super placeSubviews];
    
    if (!_imgView) {
        
        _imgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, ImageWidth, ImageWidth)];
        _imgView.image = [UIImage imageNamed:@"refresh_origin_ball"];

        _imgView.centerX = self.width/2.0f;
        _imgView.centerY = self.height/2.0f;
        [self addSubview:_imgView];
        
        _leafView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, ImageWidth, ImageWidth)];
        _leafView.image = [UIImage imageNamed:@"refresh_origin_leaf"];
        [_imgView addSubview:_leafView];
        
    }
}

-(void)orginState
{
    _leafView.hidden = NO;
    
    [_imgView stopAnimating];
    _imgView.image = [UIImage imageNamed:@"refresh_origin_ball"];
    
    _leafView.frame = CGRectMake(0, 0, ImageWidth, ImageWidth);
}

- (void)setState:(MJRefreshState)state
{
    MJRefreshCheckState
    
    [self orginState];
    
    // 根据状态做事情
    if (state == MJRefreshStateIdle) { //回收越过临界值
        
        if (oldState == MJRefreshStateRefreshing) {

        } else {
            
        }
        
    } else if (state == MJRefreshStatePulling) {//下拉越过临界值
        
    }
    
    else if (state == MJRefreshStateRefreshing) {
        
        
        //执行变身
        _imgView.animationImages = _changeArray;
        // 设置播放周期时间
        _imgView.animationDuration = MJRefreshDelay;
        // 设置播放次数
        _imgView.animationRepeatCount = 1;
        
//        _imgView.image = _loadingArray.firstObject;
        _imgView.image = [UIImage imageNamed:@"refresh_circle_25"];
        
        _imgView.width = _imgView.height = ImageWidth;
        
        _imgView.centerX = self.width/2.0f;
        _imgView.centerY = self.height/2.0f;
        
        _leafView.hidden = YES;
        
        [_imgView startAnimating];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(MJRefreshDelay * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            if (self.state == MJRefreshStateRefreshing) {
                //执行刷新
                _imgView.animationImages = _loadingArray;
                _imgView.animationDuration = 0.8;
                // 设置播放次数
                _imgView.animationRepeatCount = 0;
                //            [_imgView stopAnimating];
                [_imgView startAnimating];
                
                _imgView.image = _changeArray.lastObject;
            }
            
        });
        
        
    }
}

- (void)scrollViewContentOffsetDidChange:(NSDictionary *)change
{
    [super scrollViewContentOffsetDidChange:change];
    
    
    CGPoint new = [change[@"new"] CGPointValue];
    float cY = fabs(new.y)-_scrollView.contentInset.top;
    float scale = fabs(cY/37.0f);
    
    
    if (self.state != MJRefreshStateRefreshing) {
        
        scale = scale<1?1:scale;
        
        _imgView.height = ImageWidth *scale;
        _imgView.width = ImageWidth/scale;
        
        _imgView.centerX = self.width/2.0f;
        _imgView.centerY = self.height/2.0f-(scale-1)*ImageWidth/2.0f;
        
        
        float width = _imgView.width;
        
        width = width<45?45:width;
        
        _leafView.width = _leafView.height = width;
        
        float centerX = _imgView.width/2.0f - scale;
        centerX = centerX<-2?-2:centerX;
        
        _leafView.centerX = centerX;
        
        _leafView.centerY = _imgView.height/2.0f;
                
    }
}

@end
